import { HikvisionBaseService } from "../base";
import { hikvision } from "../type";

/** 组织信息管理 */
export class ResourceOrgService extends HikvisionBaseService {
	/** 批量添加组织 */
	async batchAdd(data: hikvision.isc.IAddOrgObject[]) {
		const url = `${this.config.baseUrl}/api/resource/v1/org/batch/add`;
		const res = await this.curl<hikvision.isc.IResponse<hikvision.isc.IAddOrgResponse>>(url, {
			method: "POST",
			dataType: "json",
			contentType: "json",
			headers: {
				access_token: this.accessToken,
			},
			data,
		});
		this.handleResponseException(res, `批量添加组织出错`);
		return res.data.data;
	}

	/** 修改组织
	 * - 仅能修改组织名称
	 */
	async update(orgIndexCode: string, orgName: string) {
		const url = `${this.config.baseUrl}/api/resource/v1/org/single/update`;
		const res = await this.curl<hikvision.isc.IResponse>(url, {
			method: "POST",
			dataType: "json",
			contentType: "json",
			headers: {
				access_token: this.accessToken,
			},
			data: {
				orgIndexCode,
				orgName,
			},
		});
		this.handleResponseException(res, `修改组织信息失败`);
		return true;
	}

	async batchDelete(indexCodes: string[]) {
		if(indexCodes.length === 0) throw new Error(`batch delete indexCodes is empty`)
		const url = `${this.config.baseUrl}/api/resource/v1/org/batch/delete`;
		type ResObj = { orgIndexCode: string; code: string; msg: string };
		const res = await this.curl<hikvision.isc.IResponse<ResObj[]>>(url, {
			method: "POST",
			dataType: "json",
			contentType: "json",
			headers: {
				access_token: this.accessToken,
			},
			data: {
				indexCodes,
			},
		});
		this.handleResponseException(res, `批量删除部门出错`);
		return res.data.data;
	}

	/** 获取根组织 */
	async getRootOrg() {
		const url = `${this.config.baseUrl}/api/resource/v1/org/rootOrg`;
		const res = await this.curl<hikvision.isc.IResponse<hikvision.isc.IOrgObject>>(url, {
			method: "GET",
			dataType: "json",
			contentType: "json",
			headers: {
				access_token: this.accessToken,
			},
		});
		this.handleResponseException(res, `获取根组织出错`);
		return res.data.data;
	}

	/** 获取组织列表 */
	async getOrgList(pageNo: number, pageSize = 100) {
		const url = `${this.config.baseUrl}/api/resource/v1/org/orgList`;
		type ResObj = {
			total: number;
			pageNo: number;
			pageSize: number;
			list: hikvision.isc.IOrgObject[];
		};
		const res = await this.curl<hikvision.isc.IResponse<ResObj>>(url, {
			method: "POST",
			dataType: "json",
			contentType: "json",
			headers: {
				access_token: this.accessToken,
			},
			data: {
				pageNo,
				pageSize,
			},
		});
		this.handleResponseException(res, `获取组织列表出错`);
		return res.data.data;
	}

	/** 获取下级组织列表，根据父组织编号 */
	async getListByParentIndexCode(parentIndexCode: string, pageNo = 1, pageSize = 200) {
		const url = `${this.config.baseUrl}/api/resource/v1/org/parentOrgIndexCode/subOrgList`;
		type ResObj = {
			total: number;
			pageNo: number;
			pageSize: number;
			list: hikvision.isc.IOrgObject[];
		};
		const res = await this.curl<hikvision.isc.IResponse<ResObj>>(url, {
			method: "POST",
			dataType: "json",
			contentType: "json",
			headers: {
				access_token: this.accessToken,
			},
			data: {
				parentOrgIndexCode: parentIndexCode,
				pageNo,
				pageSize,
			},
		});
		this.handleResponseException(res, `获取组织列表出错`);
		return res.data.data;
	}

	/** 获取组织详情，根据组织code */
	async getOrgInfoDetailByCode(orgIndexCodes: string[]) {
		const url = `${this.config.baseUrl}/api/resource/v1/org/orgIndexCodes/orgInfo`;

		type ResObj = {
			total: number;
			list: hikvision.isc.IOrgDetailObject[];
		};

		const res = await this.curl<hikvision.isc.IResponse<ResObj>>(url, {
			method: "POST",
			dataType: "json",
			contentType: "json",
			headers: {
				access_token: this.accessToken,
			},
			data: {
				orgIndexCodes,
			},
		});
		this.handleResponseException(res, `获取组织详情出错`);
		return res.data.data;
	}
}
